//
//  ViewController.m
//  UI4-UICollectionView
//
//  Created by 郭宝 on 16/8/2.
//  Copyright © 2016年 郭宝. All rights reserved.
//

#import "ViewController.h"

@interface ViewController () <UICollectionViewDelegate, UICollectionViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // itemSize 默认大小50 50
    layout.itemSize = CGSizeMake(CGRectGetWidth(self.view.bounds) / 2 - 7.5, 120);
    // NSLog(@"%@", NSStringFromCGSize(layout.itemSize));
    // 水平滚动/默认:竖直滚动
    // layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // 滚动方向改变, 行列改变
    // 最小行间距 改变宽窄
    layout.minimumLineSpacing = 5;
    // 列间距
    layout.minimumInteritemSpacing = 5;
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    // 6.区头大小 横向滚动时, 设置宽; 纵向滚动时, 设置高
    layout.headerReferenceSize = CGSizeMake(100, 100);
    // 区尾大小 横向滚动时, 设置宽; 纵向滚动时, 设置高
    layout.footerReferenceSize = CGSizeMake(200, 200);
    // 区头悬浮效果(iOS9之后出的)
    layout.sectionHeadersPinToVisibleBounds = YES;
    // 区尾悬浮效果
    layout.sectionFootersPinToVisibleBounds = YES;
    // UICollectionView创建
    // 参数1:frame 参数2:填入不能为空的layout(布局)
    UICollectionView *myCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    
    [self.view addSubview:myCollectionView];
    myCollectionView.backgroundColor = [UIColor redColor];
    myCollectionView.delegate = self;
    myCollectionView.dataSource = self;
    [self.view addSubview:myCollectionView];
    [myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    [myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reuseableView"];
    [myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"reuseableViewFoot"];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 6;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor grayColor];
    return cell;

}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
    UICollectionReusableView *reuseableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reuseableView" forIndexPath:indexPath];
        reuseableView.backgroundColor = [UIColor blueColor];
        return reuseableView;
    } else {
        UICollectionReusableView *reuseableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"reuseableViewFoot" forIndexPath:indexPath];
        reuseableView.backgroundColor = [UIColor yellowColor];
        return reuseableView;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
